package com.chijun.mform.field.impl;


import android.content.Context;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chijun.mform.create.field.IJiSuanGongShiListener;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.field.view.InstallJiSuanGongShi;
import com.chijun.mform.full.Calculator;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.MoneyUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.message.UiMessageUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.ObservableEmitter;

/**
 * 计算公式
 * 写法:
 * <字段编号>运算符号<字段编号>运算符号<字段编号>运算符号<字段编号>
 * 如：<88888899999999>+<1111111222222>-<6666777777>
 *
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor14JiSuanGongShi extends BaseField {
    public FieldFor14JiSuanGongShi(){
        super();
    }

    Map<String,BigDecimal> jisuan;


    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());

        //文本
        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData != null) {
            writeFormClass.fieldFullContentView.setText(textFieldZanCunData);
            saveValue = textFieldZanCunData;
            writeFormClass.inputIsOK = true;
        }

        MTextView daXie = new MTextView(context);
        daXie.setTextSize(13);
        daXie.setTextIsSelectable(true);

        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        jisuan = new HashMap<>();
        LogUtils.d("原始算式：" + formField.getDefaultContent());

        if (formField.getDefaultContent() != null) {
            //注册消息监听
            UiMessageUtils.getInstance().addListener(JI_SUAN_GONG_SHI, new UiMessageUtils.UiMessageCallback() {
                @Override
                public void handleMessage(@NonNull UiMessageUtils.UiMessage uiMessage) {
                    String string = uiMessage.getObject().toString();  //消息组成为  《字段id,数字》
                    LogUtils.d("收到消息" + string);
                    StringBuilder stringBuilder = new StringBuilder();
                    String[] re = string.split(",");
                    Pair<String,String> reValue = new Pair<>(re[0],re[1]);
                    jisuan.put(reValue.first.trim(),new BigDecimal(reValue.second));

                    if (re.length == 2) {
                        //解析原始算式
                        String[] split1 = formField.getDefaultContent().split("<");

                        for (String s : split1) {
                            String[] idAndValue = s.split(">");
                            //
                            if ( s.contains(">")) {
                                if (idAndValue.length == 2 ) {
                                    //字段编号  值
                                    Pair<String,String> value = new Pair<>(idAndValue[0].trim(),idAndValue[1].trim());
                                    if (jisuan.containsKey(value.first)) {
                                        stringBuilder.append(jisuan.get(value.first)).append(value.second);
                                    } else {
                                        if (writeFormClass.formField.isCanScanCode()) {
                                            stringBuilder.append(0).append(value.second);
                                        } else {
                                            return;
                                        }
                                    }
                                } else {
                                    if (jisuan.containsKey(idAndValue[0].trim())) {
                                        stringBuilder.append(jisuan.get(idAndValue[0].trim()));
                                    } else {
                                        if (writeFormClass.formField.isCanScanCode()) {
                                            stringBuilder.append(0);
                                        } else {
                                            return;
                                        }

                                    }
                                }

                            } else {
                                for (String s1 : idAndValue) {
                                    stringBuilder.append(s1.trim());
                                }
                            }



//                            if (idAndValue.length > 0 && idAndValue[0].equals(re[0])) {
//                                try {
//                                    //字段编号  字段值
//                                    jisuan.put(idAndValue[0],new BigDecimal(re[1]));
//                                } catch (Exception e) {
//                                    writeFormClass.fieldFullContentView.setText("输入错误！");
//                                    return;
//                                }
//                            }
//                            idAndValue[0] = idAndValue[0].trim();
//
//                            if (!StringUtils.isEmpty(idAndValue[0])) {
//                                if (jisuan.containsKey(idAndValue[0])) {
//                                    idAndValue[0] = String.valueOf(jisuan.get(idAndValue[0]));
//                                } else {
//                                    if (formField.isCanScanCode()) {
//                                        LogUtils.e("不包含：" + idAndValue[0] );
//                                        return;
//                                    } else {
//                                        jisuan.put(idAndValue[0],new BigDecimal(0));
//                                    }
//                                }
//
//                                for (String s1 : idAndValue) {
//                                    stringBuilder.append(s1.trim());
//                                }
//                            }
                        }
                    }

                    LogUtils.d("解析后的算式：" + stringBuilder.toString().trim());
                    BigDecimal evaluate = Calculator.evaluate(stringBuilder.toString());

                    if (formField.isShowBig()) {
                        String s1 = MoneyUtils.lowerToUpper(evaluate);
                        daXie.setText(s1);
                    }

                    writeFormClass.fieldFullContentView.setText(String.valueOf(evaluate));
                    saveValue = String.valueOf(evaluate);
                }
            });
        }

        if (formField.isShowBig()) {
            writeFormClass.fieldFullContentLayout.setVisibility(View.VISIBLE);
            writeFormClass.fieldFullContentLayout.addView(daXie);
        }

        return writeFormClass.view;
    }


    //计算公式自动生成有权限也不能修改
    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //设置字段值
        String fieldValue = fullModel.getFieldValue();
        if (StringUtils.isNoEmpty(fieldValue)) {
            fullModel.loadContent.setText(fieldValue);
        }
        fullModel.loadContent.setKeyListener(null);
        fullModel.loadContent.setFocusable(false);
        fullModel.loadContent.setTextIsSelectable(true);//可选择

        if (fullModel.formField.isShowBig()) {
            MTextView daXie = new MTextView(context);
            daXie.setTextSize(13);
            daXie.setTextIsSelectable(true);
            BigDecimal bigDecimal = new BigDecimal(fieldValue);
            String s1 = MoneyUtils.lowerToUpper(bigDecimal);
            daXie.setText(s1);
            fullModel.addCustomControl(daXie);
        }


        return fullModel.layout;
    }



    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getNumberView());        //小数位数设置
        emitter.onNext(installClass.getIsShowBigView());     //是否显示大写金额设置
        emitter.onNext(installClass.getIsReadOnlyView());    //是否只读
        emitter.onNext(installClass.getIsMastView());        //是否必填


        InstallJiSuanGongShi installJiSuanGongShi = new InstallJiSuanGongShi(context, installClass.formField.getTimeType(),installClass.formConfig.getFormFieldList(), new IJiSuanGongShiListener() {
            @Override
            public void onResult(String saveValue, String linShi) {
                LogUtils.d("返回：" + saveValue+linShi);
                installClass.formField.setDefaultContent(saveValue);
                installClass.formField.setTimeType(linShi);
            }
        });
        emitter.onNext(installJiSuanGongShi);        //设置计算公式
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }






}
